package com.levi;

import com.levi.domain.User;
import com.levi.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @author shentong
 * @since 2022/2/8 9:03 PM
 * 使用mongoRepository操作
 */
@SpringBootTest
public class MongodemoApplicationTests1 {
    @Autowired
    private UserRepository userRepository;

    /**
     * 添加
     */
    @Test
    public void save() {
        for (int i = 1; i <= 10; i++) {
            User user = new User();
            user.setName("zhangsan" + i);
            user.setAge(i);
            user.setEmail("123456@163.com");
//            user.setCreateDate("20220208");
            userRepository.save(user);
        }
    }

    /**
     *  查询所有数据
     */
    @Test
    public void findUsers() {
        List<User> users = userRepository.findAll();
        System.out.println(users);
    }

    /**
     * 根据id查询
     */
    @Test
    public void findById() {
        Optional<User> optional = userRepository.findById("62026b7f3595f32086c97970");
        optional.ifPresent(System.out::println);
    }

    /**
     * 条件查询
     */
    @Test
    public void findUsersByCondition() {
        User user = new User();
        user.setName("zhangsan8");
        user.setAge(8);
        Example<User> userExample = Example.of(user);
        List<User> users = userRepository.findAll(userExample);
        System.out.println(users);

    }

    /**
     * 模糊查询
     */
    @Test
    public void findLike() {
        User user = new User();
        user.setName("Zhang");
//        user.setAge(8);
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//模糊匹配字符串
                .withIgnoreCase(true);//忽略大小写
        Example<User> userExample = Example.of(user,exampleMatcher);
        List<User> users = userRepository.findAll(userExample);
//        System.out.println(users);
        users.forEach(System.out::println);
    }

    /**
     * 分页查询
     */
    @Test
    public void findPage() {
        User user = new User();
        user.setName("Zhang");
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//模糊匹配字符串
                .withIgnoreCase(true);//忽略大小写
        Example<User> userExample = Example.of(user,exampleMatcher);
        Pageable pageable = PageRequest.of(0, 4);
        Page<User> page = userRepository.findAll(userExample, pageable);
        System.out.println(page.getContent());
        System.out.println(page.getTotalPages());
    }

    /**
     * 修改
     */
    @Test
    public void update() {
        Optional<User> option = userRepository.findById("62026b7f3595f32086c97970");
        if (option.isPresent()) {
            User user = option.get();
            user.setName("lufei");
            userRepository.save(user);
        }
    }

    /**
     * 删除
     */
    @Test
    public void remove() {
        userRepository.deleteById("62026b7f3595f32086c97970");
    }

    @Test
    public void test2() {
        List<User> users = userRepository.findByNameAndAge("lufei", 20);
        users.forEach(System.out::println);
    }

    @Test
    public void test3() {
        List<User> users = userRepository.findUserByAgeBetween(1, 19);
        users.forEach(System.out::println);
    }

    @Test
    public void saveOne() {
        User user = new User();
        user.setName("lufei");
        user.setEmail("123456@163.com");
        user.setAge(25);
        user.setCreateDate(LocalDateTime.now());
        User u = userRepository.save(user);
        System.out.println("id: "+u.getId());
    }
}
